#include <SDL2/SDL.h>
#include <iostream>

// sdl初始化并创建窗口
bool init();

// 加载图片
bool loadMedia();

// 关闭sdl并释放资源
void close();

// 渲染的窗口
SDL_Window * gWindow = nullptr;

// 窗口表面
SDL_Surface * gScreenSurface = nullptr;

// 要加载展示的图片
SDL_Surface * gHelloWorld = nullptr;

const int SCREEN_WIDTH = 640;
const int SCREEN_HEIGHT = 480;

bool init()
{
     //Initialization flag
    bool success = true;

    //Initialize SDL
    if( SDL_Init( SDL_INIT_VIDEO ) < 0 )
    {
        char str[1024];
        sprintf(str, "SDL could not initialize! SDL_Error: %s\n", SDL_GetError());
        std::cout << str << std::endl;
        success = false;
    }
    else
    {
        //Create window
        gWindow = SDL_CreateWindow( "SDL Tutorial", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN );
        if( gWindow == nullptr )
        {
            char str[1024];
            sprintf(str, "Window could not be created! SDL_Error: %s\n", SDL_GetError());
            std::cout << str << std::endl;
            success = false;
        }
        else
        {
            //Get window surface
            gScreenSurface = SDL_GetWindowSurface( gWindow );
        }
    }

    return success;
}

bool loadMedia()
{
    //Loading success flag
    bool success = true;

    //Load splash image
    gHelloWorld = SDL_LoadBMP("helloworld.bmp");
    if( gHelloWorld == nullptr )
    {
        char str[1024];
        sprintf(str, "Unable to load image %s! SDL Error: %s\n", "helloworld.bmp", SDL_GetError());
        std::cout << str << std::endl;
        success = false;
    }

    return success;
}

void close()
{
    //Deallocate surface
    SDL_FreeSurface( gHelloWorld );
    gHelloWorld = nullptr;

    //Destroy window
    SDL_DestroyWindow( gWindow );
    gWindow = nullptr;

    //Quit SDL subsystems
    SDL_Quit();
}


int main( int argc, char* args[] )
{
    //Start up SDL and create window
    if( !init() )
    {
        std::cout << "Failed to initialize!" << std::endl;
    }
    else
    {
        //Load media
        if( !loadMedia() )
        {
            std::cout << "Failed to load media!" << std::endl;
        }
        else
        {
            //Apply the image
            SDL_BlitSurface( gHelloWorld, NULL, gScreenSurface, NULL );

             //Update the surface
            SDL_UpdateWindowSurface( gWindow );

            //Hack to get window to stay up
            SDL_Event e; 
            bool quit = false; 
            while(!quit)
            { 
                /*
                    SDL_PollEvent 的作用是从事件队列中获取最新的事件，并将事件中的数据放入我们传入函数的 SDL_Event
                    SDL_PollEvent 将不断地从队列中删除事件，直到队列为空。当队列为空时，SDL_PollEvent 将返回 0
                */
                while( SDL_PollEvent( &e ) )
                { 
                    if( e.type == SDL_QUIT ) 
                    {
                        quit = true; 
                    }
                } 
            }
        }
    }

    //Free resources and close SDL
    close();

    return 0;
}


